﻿using Microsoft.Extensions;
using Serilog.Events;

namespace KAs.Logger;

public class LogService : ILogService
{
    public void Log(string message, LogBusinessTypeEnum logBusinessType, string businessKey, LogEventLevel logEventLevel)
    {
        var dicPathList = new List<string> { logBusinessType.ToString() };
        if (businessKey.IsNullOrEmpty()) businessKey = "Default";
        dicPathList.Add(businessKey);
        dicPathList.Add(logEventLevel.ToString());
        var logger = Serilog.Log.ForContext("FileName", string.Join("/", dicPathList));
        switch (logEventLevel)
        {
            case LogEventLevel.Warning:
                logger.Warning(message);
                break;
            case LogEventLevel.Error:
                logger.Error(message);
                break;
            case LogEventLevel.Fatal:
                logger.Fatal(message);
                break;
            case LogEventLevel.Information:
            default:
                logger.Information(message);
                break;
        }
    }

    public void LogError(string message, LogBusinessTypeEnum logBusinessType, string businessKey)
    {
        Log(message, logBusinessType, businessKey, LogEventLevel.Error);
    }

    public void LogFatal(string message, LogBusinessTypeEnum logBusinessType, string businessKey)
    {
        Log(message, logBusinessType, businessKey, LogEventLevel.Fatal);
    }

    public void LogInformation(string message, LogBusinessTypeEnum logBusinessType, string businessKey)
    {
        Log(message, logBusinessType, businessKey, LogEventLevel.Information);
    }

    public void LogWarning(string message, LogBusinessTypeEnum logBusinessType, string businessKey)
    {
        Log(message, logBusinessType, businessKey, LogEventLevel.Warning);
    }
}
